# input Stata file
 
library(foreign)
Country <- read.dta("Data2_3.dta")
Country$GDPpc=Country$GDP/Country$Population
names(Country)
head (Country , 6)
theme_set(theme_bw())
Country[Country== 0.0000000000] <- NA

Country$LogGDPpc <- (log(Country$GDPpc))

Gender <- Country[, c(25,24,6, 27,29,19,7)]
head(Gender, 6)

#library(Hmisc)
#label(Gender$D_gender) <- "Discriminatory attitudes"
#label(Gender$PD_gender) <- "Perceived discrimination"
#label(Gender$Gender_gap) <- "% Female in workforce"
#label(Gender$v1) <- "Gender equality index"
#label(Gender$LogGDPpc) <- "Ln of GDP per capita in PPP"
#label(Gender$Gini) <- "Gini index disposable income"
#label(Gender$Unemployment) <- "% Unemployment"
install.packages("ggplot2")
install.packages("GGally")
install.packages("MASS")
install.packages("extrafont")
library(ggplot2)
library(GGally)
library(MASS)
library(extrafont)

colnames(Gender) <- make.names(c(
                  'Discriminatory attitudes', 
                  'Perceptions of discrimination', 
                  'Share of Female in workforce',
			'Gender equality index',
			'Log of per capita GDP in PPP', 
                  'Gini index disposable income',
                  'Unemployment rate'
))


cleanname = function(y,lab="\n"){
  sapply(y, function(c){ 
    paste(unlist(strsplit(as.character(c) , split="_")),collapse=lab)
  })
  }

colnames(Gender) = cleanname(colnames(Gender))

my_custom_cor <- function(data, mapping, color = I("grey30"), sizeRange = c(1, 5), ...) {

  #get the x and y data to use the other code
  x <- GGally::eval_data_col(data, mapping$x)
  y <- GGally::eval_data_col(data, mapping$y)

  ct <- cor.test(x,y)
  sig <- symnum(
    ct$p.value, corr = FALSE, na = FALSE,
    cutpoints = c(0, 0.001, 0.01, 0.05, 0.1, 1),
    symbols = c("***", "**", "*", ".", " ")
  )

  r <- unname(ct$estimate)
  rt <- format(r, digits=2)[1]

  # since we can't print it to get the strsize, just use the max size range
  cex <- max(sizeRange)

  # helper function to calculate a useable size
  percent_of_range <- function(percent, range) {
    percent * diff(range) + min(range, na.rm = TRUE)
  }

  # plot the cor value
  ggally_text(
    label = as.character(rt), 
    mapping = aes(),
    xP = 0.5, yP = 0.5, 
    size = 9,
     text = element_text(family = "Century Schoolbook"),
    color = color,
    ...
  ) + 
    # add the sig stars
    geom_text(
      aes_string(
        x = 0.8,
        y = 0.8
      ),
      label = sig, 
      size = I(cex),
      color = color,
      ...
    ) + 
    # remove all the background stuff and wrap it with a dashed line
    theme_classic(base_family = "Century Schoolbook") + 
    theme(
          text = element_text(family = "Century Schoolbook"),
          panel.background = element_rect(
        color = color, 
        linetype = "blank"
      ), 
      axis.line = element_blank(), 
      axis.ticks = element_blank(), 
      axis.text.y = element_blank(), 
      axis.text.x = element_blank()
    )
}

gGender <- ggpairs(Gender , 
title = "Macro-level correlates of Gender attitudes",
lower = list(continuous = wrap("smooth" , alpha = 0.5, size=0.33, color = "grey15")),
upper = list(continuous = wrap(my_custom_cor, size=7)), 
columnLabels = colnames(Gender),   
labeller = label_wrap_gen(12))+
 theme( 
    text = element_text(family = "Century Schoolbook", size=10),
    strip.text.y = element_text(angle =0), 
    strip.placement = "outside",
    axis.text.x = element_text(angle = 0, size=8),
    axis.text.y = element_text(angle = 0, size=8),
   axis.title.y = element_text(angle=0),  
    strip.background = element_rect(fill = "white"),
    panel.grid.major = element_blank()
)
gGender <- gGender + labs(title = "Macro-level correlates of Gender attitudes", x = colnames(Gender), y = colnames(Gender))
 print(gGender)
 png(file = "Figure2_3.png")
 dev.off()